let commentform = document.querySelector('.comment-form')

function huancun() {
    localStorage.setItem('lessonname', commentform.children[1].children[1].value);
    localStorage.setItem('lessonteacher', commentform.children[2].children[1].value);
    localStorage.setItem('lessoncredit', commentform.children[3].children[1].value);
}

function sendcomment(target) {
    let XHR = new XMLHttpRequest();
    let FD = new FormData(target);
    XHR.addEventListener('load', function (event) {
        obj = JSON.parse(event.target.responseText);
        localStorage.removeItem('lessonname');
        localStorage.removeItem('lessonteacher');
        localStorage.removeItem('lessoncredit');
        window.location.href = '/';
    });
    XHR.addEventListener('error', function (event) {
        alert("提交失败，请检查网络连接并重载本页面");
    });
    XHR.open('POST', '/handlelesson/');
    XHR.send(FD);
}
if (localStorage.getItem('lessonname') != null) {
    commentform.children[1].children[1].value = localStorage.getItem('lessonname');
}
if (localStorage.getItem('lessonteacher') != null) {
}
commentform.children[2].children[1].value = localStorage.getItem('lessonteacher');
if (localStorage.getItem('lessoncredit') != null) {
    commentform.children[3].children[1].value = localStorage.getItem('lessoncredit');
}

setInterval(huancun, 500);

commentform.addEventListener('submit', function (event) {
    event.preventDefault();
    localStorage.setItem('lessonname', event.target.children[1].children[1].value);
    localStorage.setItem('lessonteacher', event.target.children[2].children[1].value);
    localStorage.setItem('lessoncredit', event.target.children[3].children[1].value);
    sendcomment(event.target);
});